package com.tyc.transcation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 类描述
 *
 * @author tyc
 * @version 1.0
 * @date 2021-11-05 11:48:26
 */
@Service
public class UserService2 {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Transactional(rollbackFor = RuntimeException.class, propagation = Propagation.REQUIRED)
    public void updateUserMoney(){
        jdbcTemplate.update("update user set money = ? where name = ?", 1, "lisi");
//        int i = 1/0;
    }

    @Transactional(rollbackFor = RuntimeException.class, propagation = Propagation.REQUIRES_NEW)
    public void updateUserMoney2(){
        jdbcTemplate.update("update user set money = ? where name = ?", 1, "lisi");
//        int i = 1/0;
    }

    @Transactional(rollbackFor = RuntimeException.class, propagation = Propagation.NESTED)
    public void updateUserMoney3(){
        jdbcTemplate.update("update user set money = ? where name = ?", 1, "lisi");
//        int i = 1/0;
    }

    @Transactional(rollbackFor = RuntimeException.class, propagation = Propagation.MANDATORY)
    public void updateUserMoney4(){
        jdbcTemplate.update("update user set money = ? where name = ?", 1, "lisi");
//        int i = 1/0;
    }

    @Transactional(rollbackFor = RuntimeException.class, propagation = Propagation.NEVER)
    public void updateUserMoney5(){
        jdbcTemplate.update("update user set money = ? where name = ?", 1, "lisi");
//        int i = 1/0;
    }

    @Transactional(rollbackFor = RuntimeException.class, propagation = Propagation.SUPPORTS)
    public void updateUserMoney6(){
        jdbcTemplate.update("update user set money = ? where name = ?", 1, "lisi");
//        int i = 1/0;
    }

    @Transactional(rollbackFor = RuntimeException.class, propagation = Propagation.NOT_SUPPORTED)
    public void updateUserMoney7(){
        jdbcTemplate.update("update user set money = ? where name = ?", 1, "lisi");
//        int i = 1/0;
    }

    public void getUserByMoney() {
        jdbcTemplate.execute("select * from user  where money = 99");
    }
}
